package mall.service;

import mall.dao.GoodsDao;
import mall.dao.GoodsDaoImpl;
import mall.model.Goods;
import mall.model.Spec;
import mall.model.Type;
import mall.model.bo.*;
import mall.model.vo.*;

import java.util.ArrayList;
import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-05-28 21:01
 *
 * */
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    public List<Type> getType() {
        return goodsDao.getType();
    }

    public List<TypeGoodsVO> getGoodsByType(String typeId) {
        return goodsDao.getGoodsByType(typeId);
    }

    /*
    *新增商品表price，stockNum需要通过specList运算得到
    * 1. 保存数据到商品表
    * 2. 拿到商品表刚刚插入的商品goodsId
    * 将该goodsId以及spec数据保存到spec表
    * */
    @Override
    public void addGoods(AddGoodsBO goodsBO) {
        List<SpecBo> specList = goodsBO.getSpecList();
        //计算price，stockNum
        Double price = specList.get(0).getUnitPrice();
        int stockNum = specList.get(0).getStockNum();
        //规格不止一种
        for (int i = 1; i < specList.size(); i++) {
            //price取最小值
            if (price > specList.get(i).getUnitPrice()) {
                price = specList.get(0).getUnitPrice();
            }
            //stockNum取最大值
            if (stockNum < specList.get(i).getStockNum()) {
                stockNum = specList.get(i).getStockNum();
            }
        }
        Goods goods = new Goods(null, goodsBO.getImg(), goodsBO.getName(), price, goodsBO.getTypeId(), stockNum, goodsBO.getDesc());
        //将数据保存到商品表
        goodsDao.addGoods(goods);
        //该id就是goods表新增的商品id
        int id = goodsDao.lastInsertId();

        //将规格数据保存到规格表
        List<Spec> specs = new ArrayList<>();
        for (SpecBo specBo : specList) {
            Spec spec = new Spec(null, specBo.getSpecName(), specBo.getStockNum(), specBo.getUnitPrice(), id);
            specs.add(spec);
        }
        goodsDao.addSpecs(specs);

    }

    @Override
    public void addType(AddTypeBO typeBO) {
        Type type = new Type();
        type.setName(typeBO.getName());
        goodsDao.addType(type);
    }

    @Override
    public GetGoodsInfoVO getGoodsInfo(String id) {
        //根据id，去数据库表goods获取goodsInfoVO数据
        GoodsInfoVO goodsInfoVO = goodsDao.goodsInfoVO(id);

        //根据id，去数据库表spec获取speccInfoVO数据
        List<SpecsInfoVO> specsInfoVOList = goodsDao.specsInfoVO(id);
        return new GetGoodsInfoVO(specsInfoVOList, goodsInfoVO);
    }

    @Override
    public void deleteGoods(String id) {
        //删除数据库表goods中对应id数据
        goodsDao.deleteGoods(id);
        //删除s数据库表pec中goodsId为id的相应数据
        goodsDao.deleteSpec(id);
    }

    @Override
    public List<NoReplyMsgVO> noReplyMsg() {
        return goodsDao.noReplyMsg();
    }

    @Override
    public List<RepliedMsgVO> repliedMsg() {

        return goodsDao.repliedMsg();
    }

    @Override
    public void reply(ReplyBO replyBO) {
        goodsDao.reply(replyBO);
    }

    @Override
    public void addSpec(AddSpecBO addSpecBO) {
        goodsDao.addSpec(addSpecBO);
    }

    @Override
    public void updateGoods(UpdateGoodsBO updateGoodsBO) {
        //跟新数据库表goods
        goodsDao.updateGoods(updateGoodsBO);
        //由于spec在添加(addSpec)的时候就已经跟新，因此不用再更新
    }
}
